// subpages0/teamRedpacket/teamIndex/teamIndex.js
var app = getApp();
var remainTimeout,showMesstimeout;
Page({
  data: {
    message:[
      {
        avatar:'',
        desc:'李大壮8分钟前组队成功'
      },
      {
        avatar: '',
        desc: '李大壮10分钟前组队成功'
      }
    ]
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.scene){
      var scene = decodeURIComponent(e.scene).split('&');
      console.log(scene);
      var secneObj = {};
      for (var i = 0; i < scene.length; i++) {
        var arr = scene[i].split('=');
        var key = arr[0];
        secneObj[key] = arr[1];
      }
      e = secneObj;
    }
    if (e && e.groupId) {
      that.setData({
        groupId: e.groupId
      })
    }
    if (e && e.mid) {
      that.setData({
        mid: e.mid,
        fromSource:'share'
      })
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.getSlient();
      that.requestDetail();
      app.requestAd(that)
    }
  },
  onShow:function(){
    var that = this;
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    }else{
      that.setData({
        slient: app.globalData.slient
      })
    }
  },
  initPage: function(){
    var that = this;
    that.onLoad();
    that.onShow();
  },
  getSlient:function(){
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient:'';
    that.setData({
      slient: slient
    })
    if (slient == 1) {
      wx.hideTabBar()
    }
  },
  hideGetuser: function () {
    var that = this;
    that.setData({
      isShowgetuser: false
    })
  },
  // 获取用户信息
  getuserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      var userdata = e.detail;
      console.log(userdata);
      var slient = that.data.slient;
      var again = false;
      if (slient == 0) {
        again = true;
      }
      app.getuserInfo(that, userdata, again);
    }
  },
  requestDetail: function () {
    var that = this;
    var data = {
      map: 'applet_redbag_detail',
      groupId: that.data.groupId ? that.data.groupId:'',
      mid: that.data.mid ? that.data.mid:''
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var groupInfo = res.data.data;
          that.setData({
            groupInfo: groupInfo,
            receiveList: groupInfo.receiveList,
            noactivity:0
          })
          if (groupInfo.receiveList.length>0){
            clearTimeout(showMesstimeout);
            that.showMessage(0);
          }
          if (groupInfo.time>0){
            clearTimeout(remainTimeout);
            that.countDjs('remainTime', groupInfo.time);
          }
        } else {
          console.log(res.data);
          that.setData({
            noactivity:1,
            noactivityTip: res.data.em
          })
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh:function(){
    var that = this;
    that.requestDetail();
  },
  // 查看我的组队
  seeMyself:function(e){
    var that = this;
    if (app.globalData.slient == 1) {
      that.setData({
        isShowgetuser: true
      })
      return;
    }
    that.setData({
      mid: '',
      fromSource: '',
      groupId:''
    })
    var type = e.currentTarget.dataset.type;
    if (type == "modalStart") {
      that.setData({
        isShowsuccess: false
      })
    }
    that.requestDetail();
  },
  // 加入组队
  requestJointeam: function () {
    var that = this;
    if (app.globalData.slient == 1) {
      that.setData({
        isShowgetuser: true
      })
      return;
    }
    var data = {
      map: 'applet_redbag_join_group',
      groupId: that.data.groupId ? that.data.groupId : ''
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          that.requestDetail();
          that.setData({
            isShowsuccess: true,
            groupId:'',
            mid:'',
            fromSource:''
          })
        } else {
          that.setData({
            isShowfail:true,
            failTip:res.data.em
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // 发起组队
  requestCreateteam: function (e) {
    var that = this;
    if (app.globalData.slient == 1) {
      that.setData({
        isShowgetuser: true
      })
      return;
    }
    var type = e.currentTarget.dataset.type;
    if (type =="modalStart"){
      that.setData({
        isShowfail: false
      })
    }
    var data = {
      map: 'applet_redbag_create_group',
      aid: that.data.groupInfo.aid ? that.data.groupInfo.aid : ''
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
          that.setData({
            mid: '',
            fromSource: '',
            groupId: ''
          })
          that.requestDetail();
        } else {
          console.log(res.data)
          if (that.data.groupInfo.otherGroupId!=0){
            wx.showModal({
              title: '',
              content: res.data.em,
              showCancel:false,
              confirmText:'查看详情',
              success(res) {
                if (res.confirm) {
                  that.setData({
                    mid: '',
                    fromSource: '',
                    groupId: ''
                  })
                  that.requestDetail();
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }else{
            app.errorTip(that, res.data.em, 2000);
          }
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // 拆红包
  requestRedbagreceive: function () {
    var that = this;
    if (app.globalData.slient == 1) {
      that.setData({
        isShowgetuser: true
      })
      return;
    }
    var data = {
      map: 'applet_redbag_receive',
      groupId: that.data.groupInfo.groupId ? that.data.groupInfo.groupId : ''
    }
    console.log(data);
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
          var groupInfo = that.data.groupInfo;
          that.setData({
            groupId: groupInfo.groupId,
            redmoney: res.data.data.money,
            isShowredpacket: true
          })
          that.requestDetail();
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // 弹窗显示隐藏
  showModal:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]: true
    })
  },
  hideModal: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]: false
    })
  },
  toIndex: function () {
    app.backHome();
  },
  toparticipantsList:function(e){
    var aid = e.currentTarget.dataset.aid;
    wx.navigateTo({
      url: '/subpages0/teamRedpacket/participantsList/participantsList?id=' + aid
    })
  },
  countDjs: function (type, leftTime) {
    var that = this;
    that.fromatTime(type, leftTime);
    if (leftTime <= 0) {
      clearTimeout(remainTimeout);
      that.requestDetail();
      return;
    }
    leftTime--;
    that.setData({
      leftTime: leftTime
    })
    remainTimeout = setTimeout(function () {
      that.countDjs(type, leftTime);
    }, 1000);
  },
  fromatTime: function (type, leftTime) {
    var that = this;
    if (leftTime > 0) {
      var days = parseInt(leftTime / 60 / 60 / 24, 10);
      var hours = parseInt(leftTime / 60 / 60 % 24, 10);
      var minutes = parseInt(leftTime / 60 % 60, 10);
      var seconds = parseInt(leftTime % 60, 10);
      days = days < 10 ? '0' + days : days;
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      var timeFormat = {
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      };
      that.setData({
        remainTime: timeFormat
      })
      if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
        clearTimeout(remainTimeout);
      }
    } else {
      var timeClear = {
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00',
      }
      that.setData({
        remainTime: timeClear
      })
      clearTimeout(remainTimeout);
    }
  },
  showMessage: function (index) {
    var that = this;
    var message = that.data.receiveList;
    if (message.length>0){
      if (index >= message.length) {
        index = 0;
      }
      that.setData({
        messageShow: message[index],
        isShowmess: true
      })
      showMesstimeout = setTimeout(function () {
        index++;
        that.setData({
          isShowmess: false
        })
        that.showMessage(index);
      }, 4000)
    }
  },
  sharePd:function(){
    var that = this;
    console.log(app.globalData.slient);
    if (app.globalData.slient == 1) {
      that.setData({
        isShowgetuser: true
      })
      return;
    }
  },
  openJoinDetail: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/subpages0/teamRedpacket/joinDetail/joinDetail',
    })
  },
  openRedRecord: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/subpages0/teamRedpacket/redpackRecord/redpackRecord',
    })
  },
  onShareAppMessage:function(){
    var that = this;
    var shareCover = that.data.groupInfo.shareImg ? that.data.groupInfo.shareImg:'';
    var shareTitle = that.data.groupInfo.shareTitle ? that.data.groupInfo.shareTitle : '';
    var groupId = that.data.groupInfo.groupId ? that.data.groupInfo.groupId:'';
    var mid = app.globalData.userInfo.mid ? app.globalData.userInfo.mid:'';
    app.getPoint(that);
    return {
      title: shareTitle,
      imageUrl: shareCover,
      path: '/subpages0/teamRedpacket/teamIndex/teamIndex?groupId=' + groupId +'&mid=' + mid
    }
  }
})